<?php
namespace app\controller;

use app\BaseController;
use app\model\Company;
use app\Model\DriverModel;
use app\model\PartnerUser;
use app\service\CarService;
use app\service\DriverService;
use app\service\LineService;
use app\service\LoginService;
use app\service\NetworkBussinessService;
use app\service\NetworksService;
use app\service\WarehouseService;
use app\utils\CaptchaUtils;
use app\validate\LoginValidate;
use think\exception\ValidateException;
use \think\facade\Request;
use think\captcha\facade\Captcha;
use think\facade\Config;

/**
 * 路线管理
 */

class Line extends BaseController
{

    /**
     * @desc 路线列表
     * @return void
     */
    public function lists(){
        $uid     = Request::param('uid/d', 0);
        $param['partner_id']     = get_partner_id($uid);
        $param['uid'] = $uid;
        $param['is_export'] = Request::param('is_export/d',0);
        $param['page']     = Request::param('page/d', 0);
        $param['limit']     = Request::param('limit/d', 10);
        $param['company_name']     = Request::param('company_name/s', '');
        $param['line_name']     = Request::param('line_name/s', '');
        $param['line_no']     = Request::param('line_no/s', '');
        $param['driver_name']     = Request::param('driver_name/s', '');
        $param['car_no']     = Request::param('car_no/s', '');
        $info=LineService::lists($param);
        returnSuccess(200,'获取成功',$info);


    }


    //新增线路信息
    public function add()
    {
        $uid     = Request::param('uid/d', 0);
        $logParam['uid']=$uid;
        $param['partner_id']     = get_partner_id($uid);
//        $param['driver_ids']     = Request::param('driver_ids/s', ''); //司机ids
//        $param['car_id']     = Request::param('car_id/d', 0); //汽车ids
        $param['defaut_driver_id']     = Request::param('defaut_driver_id/d', 0);
        $param['warehouse_ids']=Request::param('warehouse_ids/s', '');

        $param['warehouse_count']=Request::param('warehouse_count/d', 0);
        $param['network_count']=Request::param('network_count/d', 0);
        $param['community_purchase']=Request::param('community_purchase/d', 0);
        //default_car_id
        $param['default_car_id']=Request::param('default_car_id/d',0);

        //仓库或者快递网点信息
        $param['network_ids'] = Request::param('network_ids/s', '');
        $param['status'] = Request::param('status/d', 1);

        $param['line_name'] = Request::param('line_name/s', '');
        $param['transport_name'] = Request::param('transport_name/s', '');

        $param['company_id'] = Request::param('company_id/d',0);
        if(!$param['company_id'])    returnFail(21009, '公司id不能为空');
        $group_id = Company::where('id',$param['company_id'])->value('group_id');
        if(!$group_id)    returnFail(21009, '公司有误');
        $param['uid'] = '1-'.$group_id.'-'.$param['company_id'];

        $logParam['ip']=Request::ip();


        try {
            validate(\app\validate\LineValidate::class)->check([
                // 'warehouse_ids'=> $param['warehouse_ids'],
                'network_ids'=> $param['network_ids'],
//                'car_id' => $param['car_id']

            ]);
        }catch (ValidateException $e){

            returnFail(21009,$e->getError());
        }
        $data =LineService::add($param,$logParam);
        if ($data==10000){
            returnSuccess(200,'添加成功');
        }else{

            returnFail(21101,'添加失败');
        }

     }
    //修改司机信息
    public function modify()
    {

        $uid     = Request::param('uid/d', 0);
        $id     = Request::param('id/d', 0);

        $logParam['uid']=$uid;
        $param['partner_id']     = get_partner_id($uid);
       // $param['driver_ids']     = Request::param('driver_ids/s', ''); //司机ids
       // $param['car_id']     = Request::param('car_id/d', 0); //汽车ids
        $param['defaut_driver_id']     = Request::param('defaut_driver_id/d', 0);
        $param['warehouse_ids']=Request::param('warehouse_ids/s', '');

        $param['warehouse_count']=Request::param('warehouse_count/d', 0);
        $param['network_count']=Request::param('network_count/d', 0);
        $param['community_purchase']=Request::param('community_purchase/d', 0);
        //default_car_id
        $param['default_car_id']=Request::param('default_car_id/d',0);

        //仓库或者快递网点信息
        $param['network_ids'] = Request::param('network_ids/s', '');
        $param['status'] = Request::param('status/d', 1);

        $param['line_name'] = Request::param('line_name/s', '');
        $param['transport_name'] = Request::param('transport_name/s', '');

        $param['company_id'] = Request::param('company_id/d',0);
        if(!$param['company_id'])    returnFail(21009, '公司id不能为空');
        $group_id = Company::where('id',$param['company_id'])->value('group_id');
        if(!$group_id)    returnFail(21009, '公司有误');
        $param['uid'] = '1-'.$group_id.'-'.$param['company_id'];

        $logParam['ip']=Request::ip();

        try {
            validate(\app\validate\LineValidate::class)->check([
                // 'warehouse_ids'=> $param['warehouse_ids'],
                'network_ids'=> $param['network_ids'],
             //   'car_id' => $param['car_id']

            ]);
        }catch (ValidateException $e){

            returnFail(21009,$e->getError());
        }
        $data =LineService::edit($param,$logParam,$id);
        if ($data==10000){
            returnSuccess(200,'修改成功');
        }else{

            returnFail(21101,'修改失败');
        }
    }
    //删除线路信息1
    public function del()
    {

        $uid     = Request::param('uid/d', 0);
        $logParam['uid']=$uid;
        $param['partner_id']     = get_partner_id($uid);
        $id     = Request::param('id/d', 0);

         $logParam['ip']=Request::ip();

        $data =LineService::del($param,$logParam,$id);
        if ($data==10000){
            returnSuccess(200,'删除成功');
        }else{

            returnFail(21101,'删除失败');
        }

    }

    public  function  line_data()
    {
        $uid     = Request::param('uid/d', 0);
        $logParam['uid']=$uid;
        $param['partner_id']     = get_partner_id($uid);
        $param['source'] = PartnerUser::where('id',$uid)->value('source');
        //获取所有仓库
        $warehouses=WarehouseService::line_data($param);
        //获取所有网点
        $networks=NetworksService::line_data($param);
        //获取所有司机
       $drivers= DriverService::line_data($param);

        //获取所有汽车1
        $cars= CarService::line_data($param);
        returnSuccess(200,'获取成功',['warehouses'=>$warehouses,'networks'=>$networks,'drivers'=>$drivers,'cars'=>$cars]);

    }

}
